package com.onlinelearn.chatservice.service;

import com.onlinelearn.chatservice.model.ChatRoom;
import com.onlinelearn.chatservice.repository.ChatRoomRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChatRoomService {
    @Autowired
    private ChatRoomRepository chatRoomRepository;

    public ChatRoom createChatRoom(Long courseId, Long teacherId) {
        ChatRoom chatRoom = new ChatRoom();
        chatRoom.setCourseId(courseId);
        chatRoom.setTeacherId(teacherId);
        return chatRoomRepository.save(chatRoom);
    }

    public void addParticipant(Long chatRoomId, Long userId) {
        ChatRoom chatRoom = chatRoomRepository.findById(chatRoomId)
                .orElseThrow(() -> new RuntimeException("聊天室未找到"));
        chatRoom.addParticipant(userId);
        chatRoomRepository.save(chatRoom);
    }

    public List<ChatRoom> getChatRoomsByCourse(Long courseId) {
        return chatRoomRepository.findByCourseId(courseId);
    }

    public List<ChatRoom> getChatRoomsByTeacher(Long teacherId) {
        return chatRoomRepository.findByTeacherId(teacherId);
    }
} 